/**
 * Project: TacticaPescaditos
 * File: KickBall.java
 */
package org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.fsm.state;

import org.javahispano.javacup.modelo.Posicion;

import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.Player;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TacticaPescaditos;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.TeamPlayer;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.geometrics.Vector2D;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.MessageDispatcher;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram;
import org.javahispano.javacup.tacticas_aceptadas.Pescaditos.com.chidotronics.javacup.messages.Telegram.MessageType;

/**
 * @author Carlos Alberto
 * class: KickBall
 */
public class KickBall implements State<TeamPlayer> {
	
	private String name;
	
	/**
	 * 
	 */
	private KickBall() {
		this.name = "KickBall";
	}


	public static final State<TeamPlayer> instance = new KickBall();

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#enter(java.lang.Object)
	 */
	public void enter(TeamPlayer player) {			

		player.getTactica().setControllingPlayer(player);	
		MessageDispatcher dispatcher = player.getTactica().getDispatcher();

		if ( player.getTactica().getReceiver() != null  ) {
			dispatcher.dispatch(player.getId(),player.getId(), MessageType.CHANGE_STATE, ChaseBall.instance, 1);
			return;
		}

		double power = player.getPassFactor();

		Vector2D    ballTarget = player.getTactica().getGoalTarget(player.getBall().getPosition(), power,TacticaPescaditos.SHOOT_ANGLE);

		if (ballTarget != null){		
			player.kick(new Posicion(ballTarget.x,ballTarget.y),1,TacticaPescaditos.SHOOT_ANGLE);
			player.getStateMachine().changeState(Wait.instance);
			player.findSupport();
			return;			  

		} 

		Player<?> receiver;
		ballTarget = new Vector2D();

		if (player.isThreatened() ) {
			receiver = player.getTactica().findPass(player, ballTarget, power,TacticaPescaditos.PASS_ANGLE);
			if ( receiver != null ) {
				player.kick(new Posicion(ballTarget.x,ballTarget.y),power,TacticaPescaditos.PASS_ANGLE);

				dispatcher.dispatch( player.getId(),receiver.getId(), 
						Telegram.MessageType.RECEIVE_BALL,ballTarget,0);

				player.getStateMachine().changeState(Wait.instance);

				player.findSupport();
				return;
			}

		}

		player.findSupport();
		player.getStateMachine().changeState(Dribble.instance);


	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#execute(java.lang.Object)
	 */
	public void execute(TeamPlayer player) {
		player.getStateMachine().changeState(Wait.instance);
		
	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#exit(java.lang.Object)
	 */
	public void exit(TeamPlayer player) {
	}

	/* (non-Javadoc)
	 * @see com.chidotronics.javacup.fsm.state.State#onMessage(java.lang.Object, com.chidotronics.javacup.fsm.state.Telegram)
	 */
	public boolean onMessage(TeamPlayer o, Telegram telegram) {	return false; }
	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return this.name.hashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return this.name;
	}
}
